let app=getApp();
const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
const db=wx.cloud.database();
const _=db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:0,
    isPhone:true,
    flag:false,
    isJoin:false,
    isTime:true,
    interval: 3000,
    duration: 1500,
    activeColor: '#fff',
    admin:false,
    images:[
      'cloud://online-00000.6f6e-online-00000/image/home/xc1.jpg',
      'cloud://online-00000.6f6e-online-00000/image/home/xc2.jpg',
      'cloud://online-00000.6f6e-online-00000/image/home/xc3.jpg',
      'cloud://online-00000.6f6e-online-00000/image/home/xc3.jpg',
      'cloud://online-00000.6f6e-online-00000/image/home/xc6.jpg'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTotal();
    this.isAdmin();
    wx.cloud.callFunction({
      name:'getStatus'
    }).then(res=>{
      this.setData({
        isTime:res.result.status
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStatus();
  },

  getTotal(){
    db.collection('joiner').where({
      _id:_.neq('')
    }).count().then(res=>{
      this.setData({
        total:res.total
      })
    })
  },

  getStatus(){
    wx.cloud.callFunction({
      name: 'join',
      data: {
        act: 'check'
      }
    }).then(res => {
      this.setData({
        isJoin: res.result.status
      })
    })
  },

  isAdmin(){
    wx.cloud.callFunction({
      name:'isAdmin'
    }).then(res=>{
      //console.log(res.result);
      this.setData({
        admin:res.result.status
      })
    })
  },

  torec(){
    wx.navigateTo({
      url: '../record/record',
    })
  },
  radioChange(e){
    let val=e.detail.value;
    this.setData({
      isPhone:val=='0'?true:false
    })
  },


  nameFocus: function () {
    this.setData({
      flag: true
    });
  },
  nameBlur: function (e) {
    if (e.detail.value) {
      this.setData({
        name: e.detail.value
      });
    }
    else {
      this.setData({
        flag: false
      });
    }
  },

  toJion(e){
    //console.log(e);
    const { type } = e.detail.value;
    const formId=e.detail.formId;
    if(type=='0'){
      let phone=e.detail.value.phone;
      if (phone == '' || !reg.test(phone)){
        wx.showModal({
          title: '提示',
          content: '请输入正确的邮箱',
          showCancel:false
        })
        return;
      }
    }

    wx.showLoading({
      title: '正在报名',
    });

    let data={
      userInfo: app.globalData.userInfo,
      time:new Date().getTime()
    }
    if(type=='0'){
      let phone=e.detail.value.phone;
      data.phone=phone
    }
    
    //console.log(data);
    wx.cloud.callFunction({
      name:'join',
      data:{
        formId,
        data,
        act:'join'
      }
    }).then(res=>{
      wx.hideLoading();
      //console.log(res.result);
      wx.showToast({
        title:res.result.mes,
        icon:'none',
        duration:3000
      });
      this.getTotal();
      this.getStatus();
    }).catch(e=>{
      wx.hideLoading()
      console.log(e);
      wx.showToast({
        title: '报名失败,请联系管理员',
        icon:'none'
      })
    })

  },

  toResult(){
    wx.navigateTo({
      url: '../result/result',
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})